import Vue from 'vue'
import Router from 'vue-router'
import {mymessage} from "../tools/mymessage";

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'MainPage',
    component: () => import('../view/MainPage'), // 路由懒加载
    meta: {
      label: '首页',
      index: 0,
      requireAuth: false
    }
  },
  {
    path: '/MainPage',
    name: 'MainPage',
    component: () => import('../view/MainPage'),
    meta: {
      label: '首页',
      index: 0,
      requireAuth: false
    }
  },
  {
    path: '/ProductFeatures',
    name: 'ProductFeatures',
    component: () => import('../view/MainPage'),
    meta: {
      label: '产品功能',
      index: 1,
      requireAuth: false
    }
  },
  {
    path: '/InstitutionalVersion',
    name: 'InstitutionalVersion',
    component: () => import('../view/MainPage'),
    meta: {
      label: '机构版',
      index: 2,
      requireAuth: false
    }
  },
  {
    path: '/ChannelsOfCooperation',
    name: 'ChannelsOfCooperation',
    component: () => import('../view/MainPage'),
    meta: {
      label: '渠道合作',
      index: 3,
      requireAuth: false
    }
  },
  {
    path: '/RightsAndInterestsOfMembers',
    name: 'RightsAndInterestsOfMembers',
    component: () => import('../view/MainPage'),
    meta: {
      label: '会员权益',
      index: 4,
      requireAuth: false
    }
  },
  {
    path: '/HelpCenter',
    name: 'HelpCenter',
    component: () => import('../view/MainPage'),
    meta: {
      label: '帮助中心',
      index: 5,
      requireAuth: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../view/login/Login'),
    meta: {
      label: '登录',
      requireAuth: false
    }
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('../view/register/Register'),
    meta: {
      label: '注册',
      requireAuth: false
    }
  },
  {
    path: '/courseList',
    name: 'courseList',
    component: () => import('../view/course/CourseList'),
    meta: {
      label: '课程首页',
      requireAuth: true,
      index: 0
    }
  },
  {
    path: '/lessonPreparation',
    name: 'lessonPreparation',
    component: () => import('../view/course/CourseList'),
    meta: {
      label: '备课区',
      requireAuth: true,
      index: 1
    }
  },
  {
    path: '/thesisDefense',
    name: 'thesisDefense',
    component: () => import('../view/course/CourseList'),
    meta: {
      label: '论文管理',
      requireAuth: true,
      index: 2
    }
  },
  {
    path: '/courseList/userSetting',
    name: 'userSetting',
    component: () => import('../view/userSetting/UserSetting'),
    meta: {
      label: '用户设置',
      requireAuth: true,
    }
  },
  {
    path: '/courseList/courseDetail',
    name: 'courseDetail',
    component: () => import('../view/course/CourseDetail'),
    meta: {
      label: '课程内容',
      requireAuth: true,
    }
  },
  {
    path: '/courseList/member',
    name: 'member',
    component: () => import('../view/course/Member'),
    meta: {
      label: '成员',
      requireAuth: true,
    }
  },
  {
    path: '/courseList/courseDetail/checkTask',
    name: 'checkTask',
    component: () => import('../view/course/CheckTask'),
    meta: {
      label: '批阅',
      requireAuth: true,
    }
  }
]


const router = new Router({
  routes,
});

router.beforeEach(async (to, from, next) => {
  /** 判断用户是否已经登录 */
  /*if (store.getters.isLogin) {
    document.title = to.matched[0].meta.label;
    /!** 已经登录情况下访问 /login, 则直接进入 /admin *!/
    if (to.path === '/login') {
      next();
      NProgress.done()
    } else {
      next();
    }
  } else {、
  、
    if (noPermission(to.path) || (to.meta.requireAuth == false)) {
      next();
    } else {
      next('/');
      Msg.warn("请先登录");
      NProgress.done();
    }
  }*/

  document.title = to.matched[0].meta.label;
  if (sessionStorage.isLogin === "true") {
    if (haveThisPage(to.path)) {
      if (to.path === '/login') {
        next();
      } else {
        next();
      }
    } else {
      next('/');
      mymessage.error("路径出错");
    }
  } else {
    if (noPermission(to.path) || (to.meta.requireAuth === false)) {
      next();
    } else {
      next('/');
      mymessage.warning("请先登录");
    }
  }
});

function haveThisPage(path) {
  let  haveThisPage = true;
  for (let i = 0; i < routes.length; i++) {
    if (routes[i].path === path) {
      haveThisPage = true;
      break;
    } else
      haveThisPage = false;
  }
  return haveThisPage;
}

function noPermission(path) {
  return ['/404',].indexOf(path) > -1
}

export default router;
